﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Data.SqlClient;

namespace hkshort
{
    public partial class GroupInfo
    {
        private class GroupResult
        {
            public SqlString _date;
            public SqlInt32 no;
            public SqlString ticker;
            public SqlString name;
            public SqlDateTime update_time;

            public GroupResult(SqlString _date, SqlInt32 no, SqlString ticker, SqlString name, SqlDateTime update_time)                                                                                                   
            {
                this._date = _date;
                this.no = no;
                this.ticker = ticker;
                this.name = name;
                this.update_time = update_time;
            }
        }

        [SqlFunction(
       DataAccess = DataAccessKind.Read,
       SystemDataAccess = SystemDataAccessKind.Read,
       FillRowMethodName = "InitMethod",
       TableDefinition = "_date nvarchar(8), no int, ticker nvarchar(32), name nvarchar(1024), update_time datetime")]
        public static IEnumerable f_download_hkshort()
        {
            ArrayList resultCollection = new ArrayList();

            MySpider myspider = new MySpider();
            List<Notice> list = myspider.CollectNotice();
            DateTime now = DateTime.Now;

            foreach (Notice n in list)
            {
                GroupResult result = new GroupResult(n.Date, n.No, n.Ticker, n.Name, now);
                //Console.WriteLine(n.Date);
                resultCollection.Add(result);
            }

            return resultCollection;
        }

        public static void InitMethod(
                object indexParResultObj,
                out SqlString _date,
                out SqlInt32 no,
                out SqlString ticker,
                out SqlString name,
                out SqlDateTime update_time)
        {
            GroupResult IndexResult = (GroupResult)indexParResultObj;

            _date = IndexResult._date;
            no = IndexResult.no;
            ticker = IndexResult.ticker;
            name = IndexResult.name;
            update_time = IndexResult.update_time;
        }
    }
}
